//
//  BoardNameController.m
//  PicUploader
//
//  Created by Chen Zhang on 9/14/08.
//  Copyright 2008 overboming. All rights reserved.
//

#import "BoardNameController.h"
#import "PUBoard.h"
#import "PreferenceController.h"

@implementation BoardNameController


- (id)init
{
    self = [super init];
    //NSLog(@"I init done");
    myBoardNameList = [[NSMutableArray alloc] init];     
    //NSLog(@"init myboardnamelist %@",myBoardNameList);
    return self;
}



- (IBAction)addBoardNameToList:(id)sender
{
    if([[boardNameField stringValue] isEqualToString:@""])
        return;
    for(PUBoard *board in myBoardNameList){
        if([[board boardName] isEqualToString:[boardNameField stringValue]])
            return;
    }    
    //NSLog(@"add new board %@",[boardNameField stringValue]); 
    PUBoard *board = [[PUBoard alloc] initWithBoardName:[boardNameField stringValue]];
    [boardNameController addObject:board];
    
    //we will update the User Defaults
    NSData *boardNameListAsData =
    [NSKeyedArchiver archivedDataWithRootObject:myBoardNameList];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:boardNameListAsData forKey:ZCBoardNameList];
    
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc postNotificationName:ZCBoardListChangedNotification object:self];
}

- (IBAction)removeBoardNameFromList:(id)sender
{
    NSArray *selectedboardNames = [boardNameController selectedObjects];
    for(PUBoard *board in selectedboardNames){
        if([[board boardName] isEqualToString:@"Mac"])
            continue;
        [boardNameController removeObject:board];
    }
    //update defaults
    NSData *boardNameListAsData =
    [NSKeyedArchiver archivedDataWithRootObject:myBoardNameList];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:boardNameListAsData forKey:ZCBoardNameList];
    
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc postNotificationName:ZCBoardListChangedNotification object:self];
}


- (void)awakeFromNib
{
    //set tableView according to UserDefaults
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *boardNameListAsData = [defaults objectForKey:ZCBoardNameList];
    NSMutableArray *helper = [[NSKeyedUnarchiver unarchiveObjectWithData:boardNameListAsData] retain];
    for(PUBoard *board in helper){
        [boardNameController addObject:[board retain]];
    }
    //update done
    
}


@synthesize myBoardNameList;
@end
